﻿using UnityEngine;
using System;
using System.Collections.Generic;


namespace UFrame
{
    /// <summary>
    /// 一个关系的代表，主要解决UI元素之间的互斥关系
    /// </summary>
    public class UIRelation
    {
        public void Link(GameObject go)
        {
        }

        public void SetActive(bool b)
        {
        }
    }

    public class RelStringSelect
    {
        public Dictionary<string, List<GameObject>> switchRel = new Dictionary<string, List<GameObject>>();

        public void SetActive(string t, bool active)
        {
            foreach (var item in switchRel)
            {
                bool isCur = item.Key == t;
                for (int j = 0; j < item.Value.Count; j++)
                {
                    bool set = isCur && active;
                    if (item.Value[j].activeSelf != set)
                        item.Value[j].SetActive(set);
                }
            }
        }

        public class RelIntSelect
        {
            public List<List<GameObject>> switchRel = new List<List<GameObject>>();

            public void SetActive(int t, bool active)
            {
                for (int i = 0; i < switchRel.Count; i++)
                {
                    var list = switchRel[i];
                    for (int j = 0; j < list.Count; j++)
                    {
                        bool isCur = i == t;
                        bool set = isCur && active;
                        if (list[j].activeSelf != set)
                            list[j].SetActive(set);
                    }
                }
            }
        }
    }

    /// <summary>
    /// list关系
    /// </summary>
    public class RelList : IUIChain
    {
        public List<GameObject> list = new List<GameObject>();

        public RelList(params Component[] cAry)
        {
            for (int i = 0; i < cAry.Length; i++)
            {
                list.Add(cAry[i].gameObject);
            }
        }

        public RelList Link(GameObject go)
        {
            list.Add(go);
            return this;
        }

        public void SetActive(bool active)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].activeInHierarchy != active)
                {
                    list[i].SetActive(active);
                }
            }
        }
    }

    /// <summary>
    /// A,B关系
    /// </summary>
    public class RelAB : IUIChain
    {
        public List<GameObject> trueList = new List<GameObject>();
        public List<GameObject> falseList = new List<GameObject>();

        public RelAB()
        {
        }

        public RelAB(Component a, Component b)
        {
            trueList.Add(a.gameObject);
            trueList.Add(b.gameObject);
        }

        public RelAB(Component[] a, Component[] b)
        {
            for (int i = 0; i < a.Length; i++)
            {
                trueList.Add(a[i].gameObject);
            }

            for (int i = 0; i < b.Length; i++)
            {
                trueList.Add(b[i].gameObject);
            }
        }


        public void SetActive(bool active)
        {
            for (int i = 0; i < trueList.Count; i++)
            {
                if (trueList[i].activeInHierarchy != active)
                    trueList[i].SetActive(active);
            }

            for (int i = 0; i < falseList.Count; i++)
            {
                if (falseList[i].activeInHierarchy != active)
                    falseList[i].SetActive(active);
            }
        }
    }

    public interface IUIChain
    {
        void SetActive(bool active);
    }
}